package javolution;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import javolution.lang.Configurable;
import javolution.testing.TestCase;
import javolution.testing.TestContext;
import javolution.testing.TestSuite;
import javolution.text.TextBuilder;
import javolution.util.FastList;
import javolution.util.FastMap;
import javolution.util.FastSet;
import javolution.util.FastTable;
import javolution.util.Index;

/* loaded from: classes2.dex */
public final class UtilTestSuite extends TestSuite {
    private static final int PADDING = 60;
    public static final Configurable<Integer> SIZE = new Configurable<>(new Integer(256));

    /* loaded from: classes2.dex */
    public static class ArrayListDirectIteration extends TestCase {
        private final ArrayList _list = new ArrayList();
        private final int _size;

        public ArrayListDirectIteration(int i) {
            this._size = i;
            for (int i2 = 0; i2 < this._size; i2++) {
                this._list.add(Index.valueOf(i2));
            }
        }

        @Override // javolution.testing.TestCase
        public int count() {
            return this._size;
        }

        @Override // javolution.testing.TestCase
        public void execute() {
            Index valueOf = Index.valueOf(this._size - 1);
            for (int i = 0; i < this._size; i++) {
                if (this._list.get(i) == valueOf) {
                    return;
                }
            }
            throw new Error();
        }

        @Override // javolution.testing.TestCase
        public CharSequence getDescription() {
            return TextBuilder.newInstance().append("java.util.ArrayList.get(i)").toText().padRight(60);
        }

        @Override // javolution.testing.TestCase
        public void validate() {
            for (int i = 0; i < this._size && TestContext.assertEquals(Index.valueOf(i), this._list.get(i)); i++) {
            }
        }
    }

    /* loaded from: classes2.dex */
    public static class CollectionAdd extends TestCase {
        private final Class _class;
        private Collection _collection;
        private final boolean _reuse;
        private final int _size;

        public CollectionAdd(Class cls, int i, boolean z) {
            this._class = cls;
            this._size = i;
            this._reuse = z;
        }

        @Override // javolution.testing.TestCase
        public int count() {
            return this._size;
        }

        @Override // javolution.testing.TestCase
        public void execute() {
            for (int i = 0; i < this._size; i++) {
                this._collection.add(Index.valueOf(i));
            }
        }

        @Override // javolution.testing.TestCase
        public CharSequence getDescription() {
            return TextBuilder.newInstance().append(this._reuse ? "Recycled " : "New ").append(this._class.getName()).append(".add(element)").toText().padRight(60);
        }

        @Override // javolution.testing.TestCase
        public void prepare() {
            Collection collection;
            if (!this._reuse || (collection = this._collection) == null) {
                this._collection = (Collection) UtilTestSuite.newInstanceOf(this._class);
            } else {
                collection.clear();
            }
        }

        @Override // javolution.testing.TestCase
        public void validate() {
            int i = 0;
            TestContext.assertTrue(this._collection.size() == this._size);
            while (i < this._size) {
                int i2 = i + 1;
                if (!TestContext.assertTrue(this._collection.contains(Index.valueOf(i)))) {
                    return;
                }
                i = i2;
            }
        }
    }

    /* loaded from: classes2.dex */
    public static class CollectionIteration extends TestCase {
        private final Class _class;
        private final Collection _collection;
        private int _count;
        private final int _size;

        public CollectionIteration(Class cls, int i) {
            this._class = cls;
            this._size = i;
            this._collection = (Collection) UtilTestSuite.newInstanceOf(cls);
            for (int i2 = 0; i2 < this._size; i2++) {
                this._collection.add(Index.valueOf(i2));
            }
        }

        @Override // javolution.testing.TestCase
        public int count() {
            return this._count;
        }

        @Override // javolution.testing.TestCase
        public void execute() {
            this._count = 0;
            Iterator it = this._collection.iterator();
            while (it.hasNext()) {
                it.next();
                this._count++;
            }
        }

        @Override // javolution.testing.TestCase
        public CharSequence getDescription() {
            return TextBuilder.newInstance().append("Iterates over ").append(this._class.getName()).toText().padRight(60);
        }

        @Override // javolution.testing.TestCase
        public void validate() {
            TestContext.assertTrue(this._count == this._size);
        }
    }

    /* loaded from: classes2.dex */
    public static class FastListDirectIteration extends TestCase {
        private final FastList _list = new FastList();
        private final int _size;

        public FastListDirectIteration(int i) {
            this._size = i;
            for (int i2 = 0; i2 < this._size; i2++) {
                this._list.add(Index.valueOf(i2));
            }
        }

        @Override // javolution.testing.TestCase
        public int count() {
            return this._size;
        }

        @Override // javolution.testing.TestCase
        public void execute() {
            Index valueOf = Index.valueOf(this._size - 1);
            FastList.Node head = this._list.head();
            FastList.Node tail = this._list.tail();
            do {
                head = head.getNext();
                if (head == tail) {
                    throw new Error();
                }
            } while (head.getValue() != valueOf);
        }

        @Override // javolution.testing.TestCase
        public CharSequence getDescription() {
            return TextBuilder.newInstance().append("javolution.util.FastList.Node.getNext()").toText().padRight(60);
        }

        @Override // javolution.testing.TestCase
        public void validate() {
            FastList.Node head = this._list.head();
            FastList.Node tail = this._list.tail();
            int i = 0;
            while (true) {
                head = head.getNext();
                if (head == tail || !TestContext.assertEquals(Index.valueOf(i), head.getValue())) {
                    break;
                }
                i++;
            }
            TestContext.assertTrue(i == this._size);
        }
    }

    /* loaded from: classes2.dex */
    public static class FastMapDirectIteration extends TestCase {
        private final FastMap _map = new FastMap();
        private final int _size;

        public FastMapDirectIteration(int i) {
            this._size = i;
            for (int i2 = 0; i2 < this._size; i2++) {
                this._map.put(Index.valueOf(i2), Index.valueOf(i2));
            }
        }

        @Override // javolution.testing.TestCase
        public int count() {
            return this._size;
        }

        @Override // javolution.testing.TestCase
        public void execute() {
            Index valueOf = Index.valueOf(this._size - 1);
            FastMap.Entry head = this._map.head();
            FastMap.Entry tail = this._map.tail();
            do {
                head = head.getNext();
                if (head == tail) {
                    throw new Error();
                }
            } while (head.getKey() != valueOf);
        }

        @Override // javolution.testing.TestCase
        public CharSequence getDescription() {
            return TextBuilder.newInstance().append("javolution.util.FastMap.Entry.getNext()").toText().padRight(60);
        }

        @Override // javolution.testing.TestCase
        public void validate() {
            FastMap.Entry head = this._map.head();
            FastMap.Entry tail = this._map.tail();
            int i = 0;
            while (true) {
                head = head.getNext();
                if (head == tail || !TestContext.assertEquals(Index.valueOf(i), head.getValue())) {
                    break;
                }
                i++;
            }
            TestContext.assertTrue(i == this._size);
        }
    }

    /* loaded from: classes2.dex */
    public static class FastTableDirectIteration extends TestCase {
        private final FastTable _list = new FastTable();
        private final int _size;

        public FastTableDirectIteration(int i) {
            this._size = i;
            for (int i2 = 0; i2 < this._size; i2++) {
                this._list.add(Index.valueOf(i2));
            }
        }

        @Override // javolution.testing.TestCase
        public int count() {
            return this._size;
        }

        @Override // javolution.testing.TestCase
        public void execute() {
            Index valueOf = Index.valueOf(this._size - 1);
            for (int i = 0; i < this._size; i++) {
                if (this._list.get(i) == valueOf) {
                    return;
                }
            }
            throw new Error();
        }

        @Override // javolution.testing.TestCase
        public CharSequence getDescription() {
            return TextBuilder.newInstance().append("javolution.util.FastTable.get(i)").toText().padRight(60);
        }

        @Override // javolution.testing.TestCase
        public void validate() {
            for (int i = 0; i < this._size && TestContext.assertEquals(Index.valueOf(i), this._list.get(i)); i++) {
            }
        }
    }

    /* loaded from: classes2.dex */
    public static class MapGet extends TestCase {
        private final Class _class;
        private Object _last;
        private Map _map;
        private final int _size;

        public MapGet(Class cls, int i) {
            this._class = cls;
            this._size = i;
            this._map = (Map) UtilTestSuite.newInstanceOf(cls);
            int i2 = 0;
            while (i2 < this._size) {
                Map map = this._map;
                Index valueOf = Index.valueOf(i2);
                i2++;
                map.put(valueOf, Index.valueOf(i2));
            }
        }

        @Override // javolution.testing.TestCase
        public int count() {
            return this._size;
        }

        @Override // javolution.testing.TestCase
        public void execute() {
            for (int i = 0; i < this._size; i++) {
                this._last = this._map.get(Index.valueOf(i));
            }
        }

        @Override // javolution.testing.TestCase
        public CharSequence getDescription() {
            return TextBuilder.newInstance().append(this._class.getName()).append(".get(key):").toText().padRight(60);
        }

        @Override // javolution.testing.TestCase
        public void validate() {
            int i = 0;
            while (i < this._size) {
                int i2 = i + 1;
                if (!TestContext.assertEquals(Index.valueOf(i2), this._map.get(Index.valueOf(i)))) {
                    break;
                }
                i = i2;
            }
            TestContext.assertEquals(Index.valueOf(this._size), this._last);
        }
    }

    /* loaded from: classes2.dex */
    public static class MapIteration extends TestCase {
        private final Class _class;
        private Object _last;
        private final Map _map;
        private final int _size;

        public MapIteration(Class cls, int i) {
            this._class = cls;
            this._size = i;
            this._map = (Map) UtilTestSuite.newInstanceOf(cls);
            for (int i2 = 0; i2 < this._size; i2++) {
                this._map.put(Index.valueOf(i2), Index.valueOf(i2));
            }
        }

        @Override // javolution.testing.TestCase
        public int count() {
            return this._size;
        }

        @Override // javolution.testing.TestCase
        public void execute() {
            for (Object obj : this._map.entrySet()) {
                this._last = obj;
            }
        }

        @Override // javolution.testing.TestCase
        public CharSequence getDescription() {
            return TextBuilder.newInstance().append("Iterates over ").append(this._class.getName()).toText().padRight(60);
        }

        @Override // javolution.testing.TestCase
        public void validate() {
            Map.Entry entry = (Map.Entry) this._last;
            TestContext.assertTrue(entry.getKey() == entry.getValue());
        }
    }

    /* loaded from: classes2.dex */
    public static class MapPut extends TestCase {
        private final Class _class;
        private Map _map;
        private final boolean _reuse;
        private final int _size;

        public MapPut(Class cls, int i, boolean z) {
            this._class = cls;
            this._size = i;
            this._reuse = z;
        }

        @Override // javolution.testing.TestCase
        public int count() {
            return this._size;
        }

        @Override // javolution.testing.TestCase
        public void execute() {
            for (int i = 0; i < this._size; i++) {
                this._map.put(Index.valueOf(i), "");
            }
        }

        @Override // javolution.testing.TestCase
        public CharSequence getDescription() {
            return TextBuilder.newInstance().append(this._reuse ? "Recycled " : "New ").append(this._class.getName()).append(".put(key, value)").toText().padRight(60);
        }

        @Override // javolution.testing.TestCase
        public void prepare() {
            Map map;
            if (!this._reuse || (map = this._map) == null) {
                this._map = (Map) UtilTestSuite.newInstanceOf(this._class);
            } else {
                map.clear();
            }
        }

        @Override // javolution.testing.TestCase
        public void validate() {
            int i = 0;
            TestContext.assertTrue(this._map.size() == this._size);
            while (i < this._size) {
                int i2 = i + 1;
                if (!TestContext.assertTrue(this._map.containsKey(Index.valueOf(i)))) {
                    return;
                }
                i = i2;
            }
        }
    }

    /* JADX INFO: Access modifiers changed from: private */
    /* loaded from: classes2.dex */
    public static final class SharedFastMap extends FastMap {
        SharedFastMap() {
            setShared(true);
        }
    }

    /* JADX INFO: Access modifiers changed from: private */
    public static Object newInstanceOf(Class cls) {
        try {
            return cls.newInstance();
        } catch (IllegalAccessException e) {
            throw new JavolutionError(e);
        } catch (InstantiationException e2) {
            throw new JavolutionError(e2);
        }
    }

    @Override // javolution.testing.TestSuite, java.lang.Runnable
    public void run() {
        int intValue = SIZE.get().intValue();
        Index.setMinimumRange(0, intValue);
        TestContext.info("----------------------------------------------");
        TestContext.info("-- Test Suite for javolution.util.* classes --");
        TestContext.info("----------------------------------------------");
        TestContext.info("Collections/Maps of " + intValue + " elements (configurable \"javolution.UtilTestSuite#SIZE\")");
        TestContext.info("");
        TestContext.info(" - Add elements to collection -");
        TestContext.test(new CollectionAdd(FastTable.class, intValue, false));
        TestContext.test(new CollectionAdd(ArrayList.class, intValue, false));
        TestContext.test(new CollectionAdd(FastList.class, intValue, false));
        TestContext.test(new CollectionAdd(LinkedList.class, intValue, false));
        TestContext.test(new CollectionAdd(FastSet.class, intValue, false));
        TestContext.test(new CollectionAdd(HashSet.class, intValue, false));
        TestContext.test(new CollectionAdd(FastTable.class, intValue, true));
        TestContext.test(new CollectionAdd(ArrayList.class, intValue, true));
        TestContext.test(new CollectionAdd(FastList.class, intValue, true));
        TestContext.test(new CollectionAdd(LinkedList.class, intValue, true));
        TestContext.test(new CollectionAdd(FastSet.class, intValue, true));
        TestContext.test(new CollectionAdd(HashSet.class, intValue, true));
        TestContext.info("");
        TestContext.info(" - Iterate over collections -");
        TestContext.test(new CollectionIteration(FastTable.class, intValue));
        TestContext.test(new CollectionIteration(ArrayList.class, intValue));
        TestContext.test(new CollectionIteration(FastList.class, intValue));
        TestContext.test(new CollectionIteration(LinkedList.class, intValue));
        TestContext.test(new CollectionIteration(FastSet.class, intValue));
        TestContext.test(new CollectionIteration(HashSet.class, intValue));
        TestContext.info("");
        TestContext.info(" - Put new key/value pairs to map instance -");
        TestContext.test(new MapPut(FastMap.class, intValue, false));
        TestContext.test(new MapPut(HashMap.class, intValue, false));
        TestContext.test(new MapPut(LinkedHashMap.class, intValue, false));
        TestContext.test(new MapPut(SharedFastMap.class, intValue, false));
        TestContext.test(new MapPut(ConcurrentHashMap.class, intValue, false));
        TestContext.test(new MapPut(FastMap.class, intValue, true));
        TestContext.test(new MapPut(HashMap.class, intValue, true));
        TestContext.test(new MapPut(LinkedHashMap.class, intValue, true));
        TestContext.info("");
        TestContext.info(" - Retrieves map value from key - ");
        TestContext.test(new MapGet(FastMap.class, intValue));
        TestContext.test(new MapGet(HashMap.class, intValue));
        TestContext.test(new MapGet(LinkedHashMap.class, intValue));
        TestContext.test(new MapGet(SharedFastMap.class, intValue));
        TestContext.test(new MapGet(ConcurrentHashMap.class, intValue));
        TestContext.info("");
        TestContext.info(" - Iterates over map entries - ");
        TestContext.test(new MapIteration(FastMap.class, intValue));
        TestContext.test(new MapIteration(HashMap.class, intValue));
        TestContext.test(new MapIteration(LinkedHashMap.class, intValue));
        TestContext.test(new MapIteration(SharedFastMap.class, intValue));
        TestContext.test(new MapIteration(ConcurrentHashMap.class, intValue));
        TestContext.info("");
        TestContext.info(" - Direct collection/map iterations (no iterator) - ");
        TestContext.test(new FastTableDirectIteration(intValue));
        TestContext.test(new ArrayListDirectIteration(intValue));
        TestContext.test(new FastListDirectIteration(intValue));
        TestContext.test(new FastMapDirectIteration(intValue));
        TestContext.info("");
    }
}
